package com.junmeng.libfloating.base

import android.app.Activity
import android.view.View
import com.junmeng.lib.ui.topview.core.WindowTopViewManager
import com.junmeng.libfloating.ball.SimpleFloatingViewFactory
import com.junmeng.libfloating.util.Log
import com.junmeng.libfloating.util.ViewUtil

class FloatingBall(var builder: Builder) : IFloating {
    companion object {
        @JvmStatic
        fun newBuilder(): Builder {
            return Builder()
        }
    }

    private var isEnable = false

    private var listener: FloatingManager.Listener = object : FloatingManager.Listener {
        override fun onAdd(type: String, child: View, viewFactory: IFloatingViewFactory) {
            if (builder.filterActivitys.contains(WindowTopViewManager.getCurrentActivity()?.javaClass?.canonicalName)) {
                Log.i("onAdd:${child.context.javaClass.canonicalName} 被过滤")
                WindowTopViewManager.unapplyViewFactoryOnlyCurrent(builder.viewFactory.getType())
            }
        }

        override fun onFirstActivityCreated(firstActivity: Activity) {
            if (isEnable) {
                WindowTopViewManager.applyViewFactory(builder.viewFactory.getType())
            }
        }

    }


    init {
        //默认不开启，调用enable(true)时才开启
//        FloatingManager.getInstance().addAttachStateCallback(attachStateCallback)
    }

    class Builder() {
        var id: String = ""
        var filterActivitys: MutableSet<String> = hashSetOf()
        var viewFactory: IFloatingViewFactory = SimpleFloatingViewFactory.newBuilder().build()

        /**
         * 设置唯一标识，不设的话默认以{@link com.junmeng.libfloating.base.FloatingBall.Builder.build}方法返回的对象的hashcode为标识
         */
        fun id(id: String): Builder {
            this.id = id
            return this
        }

        /**
         * 视图工厂，要显示在decorview内容区的视图在此工厂获得
         */
        fun viewFactory(viewFactory: IFloatingViewFactory): Builder {
            this.viewFactory = viewFactory
            return this
        }

        /**
         * 设置哪些activity不显示悬浮view，不设的话则所有activity都会显示
         */
        fun filterActivitys(set: HashSet<String>): Builder {
            filterActivitys = set
            return this
        }

        /**
         * 设置哪些activity不显示悬浮view
         */
        fun filterActivity(activityCanonicalName: String): Builder {
            filterActivitys.add(activityCanonicalName)
            return this
        }


        fun build(): FloatingBall {
            if (viewFactory == null) {
                throw RuntimeException("viewFactory不能为null")
            }
//            if (viewFactory.getView() == null) {
//                throw RuntimeException("viewFactory.getView()不能为null")
//            }
            var ball = FloatingBall(this)
            if (id.isNullOrEmpty()) {
                id("${ball.hashCode()}")
            }
            viewFactory.setAttachFloatingBallId(id)
            FloatingManager.getInstance().register(id, ball)
            WindowTopViewManager.registerViewFactory(viewFactory)
            return ball
        }
    }

    /**
     * 用户在FloatingBall对象不用时主动调用此方法，可以避免一直占用资源
     */
    override fun release() {
        FloatingManager.getInstance().unregister(builder.id)
        WindowTopViewManager.unregisterViewFactory(builder.viewFactory.getType())
    }

    override fun enable(enable: Boolean) {
        isEnable = enable
        if (isEnable) {
            FloatingManager.getInstance().addListener(listener)
            WindowTopViewManager.applyViewFactory(builder.viewFactory.getType())
        } else {
            FloatingManager.getInstance().removeListener(listener)
            WindowTopViewManager.unapplyViewFactory(builder.viewFactory.getType())
        }
    }

    override fun isEnable(): Boolean {
        return isEnable
    }

    override fun show() {
        builder.viewFactory.getView().visibility = View.VISIBLE
    }

    override fun hide() {
        builder.viewFactory.getView().visibility = View.GONE
    }

    override fun remove() {
        ViewUtil.removeViewFromParent(builder.viewFactory.getView())
    }
}